package com.zyl.Ecs.device.web.controller;

import com.zyl.Ecs.common.domain.device.ext.SensorExt;
import com.zyl.Ecs.common.domain.device.vo.SensorVO;
import com.zyl.Ecs.common.response.Result;
import com.zyl.Ecs.common.response.ResultCode;
import com.zyl.Ecs.device.service.SensorService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.stream.Collectors;

/**
 * @version v1.0
 * @ProjectName: Ecs
 * @Package: com.zyl.Ecs.device.web.controller
 * @ClassName: SensorController
 * @Description TODO (传感器控制器)
 * @Author 翟玉龙
 * @Date 2024/12/5-10:19
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/sensor")
public class SensorController {

    private final SensorService sensorService;

    /**
     * 新增传感器
     *
     * @param sensorVO 传感器信息
     * @return {@link Result }<{@link ? }>
     */
    @PutMapping
    public Result<?> update(@RequestBody SensorVO sensorVO) {
        sensorService.update(SensorExt.toExt(sensorVO));
        return Result.success();
    }

    /**
     * 获取传感器列表
     *
     * @return {@link Result }<{@link ? }>
     */
    @GetMapping
    public Result<?> list() {
        return Result.success(sensorService.getList()
                .stream()
                .map(SensorVO::toVO)
                .collect(Collectors.toList()))
                .apply(ResultCode.SUCCESS.getMessage());
    }

    /**
     * 获取更详细传感器列表
     *
     * @return {@link Result }<{@link ? }>
     */
    @GetMapping("/ext")
    public Result<?> listMore() {
        return Result.success(sensorService.getList())
                .apply(ResultCode.SUCCESS.getMessage());
    }

    /**
     * 获取未绑定设备传感器列表
     *
     * @return {@link Result }<{@link ? }>
     */
    @GetMapping(params = "type=unbound")
    public Result<?> listUnbound() {
        return Result.success(sensorService.listUnbound()
                .stream()
                .map(SensorVO::toVO)
                .collect(Collectors.toList()))
                .apply(ResultCode.SUCCESS.getMessage());
    }

}
